package com.itheima.mall.service.umsService;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.mall.exception.ConsumerException;
import com.itheima.mall.utils.AppJwtUtil;
import com.itheima.pojo.ums.UmsAdmin;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.UmsRole;
import com.itheima.pojo.ums.UmsRoleMenuRelation;
import com.itheima.vo.ums.UmsAdminVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;

@Service
public class UmsAdminService {


    @DubboReference
    private UmsAdminApi umsAdminApi;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsRoleApi umsRoleApi;


    /**
     * 1.登录
     * @param map
     * @return
     */
    public ResponseEntity login(Map<String, String> map) {

        //1.封装在map集合中(获取从前端传过来的用户名)
        String username = map.get("username");
        //2.根据username查找用户是否存在
        UmsAdmin umsAdmin = umsAdminApi.selectByUsername(username);

        //3.如果用户不存在 则返回用户不存在
        if (ObjectUtil.isNull(umsAdmin)) {
            throw new ConsumerException("该用户不存在");
        }


        //4.获取从前端传过来的用户名 （密码需要进行加密处理后在进行处理）
        String password = map.get("password");
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //5.判断输入的密码是否正确
        if ( !StrUtil.equals(password,umsAdmin.getPassword())) {
            throw new ConsumerException("输入的密码不正确");
        }

        //6.判断用户状态是否被禁用  帐号启用状态：0->禁用；1->启用
        if (umsAdmin.getStatus() == 0) {
            throw new ConsumerException("用户已被冻结");
        }

        //根据用户id生成token
        String token = AppJwtUtil.getToken(umsAdmin.getId());
        //获取登录者id，保存到redis
        redisTemplate.opsForValue().set("ID",Convert.toStr(umsAdmin.getId()));
        return ResponseEntity.ok(token);

    }



    /**
     * 删除指定用户信息
     * @param umsAdmin
     * @return
     */
    public void deleteById(UmsAdmin umsAdmin) {
        Long id = umsAdmin.getId();

        umsAdminApi.deleteById(id);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    public UmsAdminVo info() {

        UmsAdminVo vo =new UmsAdminVo();
        //从redis中获取到指定的id
        String id = redisTemplate.opsForValue().get("ID");
        UmsAdmin umsAdmin = umsAdminApi.selectById(id);
        //根据umsAdmin 查找icon
        String icon = umsAdmin.getIcon();

        String username = umsAdmin.getUsername();

        UmsRoleMenuRelation umsRoleMenuRelation = umsRoleMenuRelationApi.selectOne(id);
        Long roleId = umsRoleMenuRelation.getRoleId();
        Long menuId = umsRoleMenuRelation.getMenuId();
        List<UmsMenu> menuList = umsMenuApi.selectByMenuId(menuId);

        UmsRole umsRole = umsRoleApi.selectByRoleId(roleId);

        String name = umsRole.getName();

        vo.setIcon(icon);
        vo.setUmsMenus(menuList);
        vo.setUsername(username);
        vo.setName(name);

        return vo;
    }
}
